Imports System.Diagnostics.Process
Imports System.Data.SqlClient
Imports System.IO
Imports System
Imports System.Text
Imports System.Windows.Forms
Imports System.Runtime.InteropServices

Public Class SQLServer

    Dim strConn As String
    Dim DataBaseName As String = "MemberDB"

    Dim NewDBLoc As String
    Private Const strWHITE_EXCLAMATION_MARK_ON_RED_TRIANGLE_ICON_NAME As String = "WhiteExclamationMarkOnRedTriangle.ico"
    Dim m_clsSLQDMO As clsSQLDMO

    Private Enum MethodOfGettingSQLServerListEnum
        NetAPI32
        SQLDMO
    End Enum

    Private Function GetListOfSQLServers(ByVal enmMethodOfGettingSQLServerList As MethodOfGettingSQLServerListEnum) As Boolean
        Dim colSQLServers As Collection
        Dim objSQLServer As Object
        Dim blnGotListOfSQLServers As Boolean = False
        Me.cboServerName.Properties.Items.Clear()
        '   Get a list of available SQL Servers on the local network segment
        Try
            '   Created anyway because list of databases is gathered using this method
            m_clsSLQDMO = New clsSQLDMO
            Select Case enmMethodOfGettingSQLServerList
                Case MethodOfGettingSQLServerListEnum.NetAPI32
                    'm_clsNetAPI32 = New clsNetAPI32
                    'blnGotListOfSQLServers = m_clsNetAPI32.GetListOfServers(colSQLServers)
                Case MethodOfGettingSQLServerListEnum.SQLDMO
                    blnGotListOfSQLServers = m_clsSLQDMO.GetListOfSQLServers(colSQLServers)
            End Select
        Catch ex As System.Exception
            MsgBox(ex.ToString)
        Finally
            If blnGotListOfSQLServers Then
                For Each objSQLServer In colSQLServers
                    Me.cboServerName.Properties.Items.Add(objSQLServer)
                Next
                Me.cboServerName.Properties.Items.Add(Environment.MachineName)
            Else
                'Call(DisplayErrorInStatusBar("Error Retrieving SQL/MSDE Database Servers"))
            End If
        End Try
    End Function

    Private Sub SQLServer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.lblComputerName.Text = Environment.MachineName
        'Get Server Name
        Call GetListOfSQLServers(MethodOfGettingSQLServerListEnum.SQLDMO)
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        If File.Exists(Application.StartupPath & "\SQLServerConnection.txt") = False Then
            MsgBox("Connection has not been established, Please Restart Application", MsgBoxStyle.Exclamation)
            End
        End If
        Me.Close()
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        If rbClient.Checked = False Then
            strConn = "workstation id= .\sqlexpress ;packet size=4096;integrated security=SSPI;data source= .\sqlexpress ;persist security info=False;initial catalog=" & DataBaseName & ""
        Else
            strConn = "Data Source=" & Me.cboServerName.Text & ";Initial Catalog=" & DataBaseName & ";User Id=" & Me.txtLoginID.Text & ";Password=" & Me.txtPassword.Text & ";"
        End If
        My.Settings.Conn = strConn
        My.Settings.Save()
        'Dim ConnFilePath As String = Application.StartupPath
        'Dim objReader As StreamWriter
        'objReader = New StreamWriter(ConnFilePath & "\SQLServerConnection.txt")
        'objReader.Write(strConn)
        'objReader.Close()
        'gConnectionStr = strConn
        SetupConnection()
        Me.Close()
    End Sub

    Private Function ConnectServer() As Boolean

        Try
            gCnnPCE = New SqlConnection(strConn)
            gCnnPCE.Open()
            Return True
            gCnnPCE.Close()
        Catch ex As System.Exception
            Return False
        End Try


    End Function

    Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
        If rbClient.Checked = False Then
            strConn = "workstation id= .\sqlexpress ;packet size=4096;integrated security=SSPI;data source=.\sqlexpress  ;persist security info=False;initial catalog=" & DataBaseName & ""
        Else
            strConn = "Data Source=" & Me.cboServerName.Text & ";Initial Catalog=" & DataBaseName & ";User Id=" & Me.txtLoginID.Text & ";Password=" & Me.txtPassword.Text & ";"
        End If

        If ConnectServer() = True Then
            MsgBox("Successfully Connect ", MsgBoxStyle.Information)
            If rbClient.Checked Then
                Me.PictureBoxClientOK.Visible = True
            Else
                Me.PictureBoxServerOK.Visible = True
            End If
            Me.btnOK.Enabled = True
        Else
            MsgBox("Fail to Connect to Database", MsgBoxStyle.Exclamation)
            Me.PictureBoxClientOK.Visible = False
            Me.PictureBoxServerOK.Visible = False
            Me.btnOK.Enabled = False
        End If
    End Sub


    Private Sub rb_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbClient.CheckedChanged, rbServer.CheckedChanged
        If rbClient.Checked = True Then
            GroupServerInfo.Enabled = True
        Else
            GroupServerInfo.Enabled = False
        End If
        Me.PictureBoxClientOK.Visible = False
        Me.PictureBoxServerOK.Visible = False
    End Sub


End Class